package ups.glre.news.service.impl;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ups.glre.news.dao.CategoryDAO;
import ups.glre.news.dao.NewsDAO;
import ups.glre.news.dao.UserDAO;
import ups.glre.news.dao.impl.NewsDAOImpl;
import ups.glre.news.jms.NewsJmsService;
import ups.glre.news.model.Category;
import ups.glre.news.model.News;
import ups.glre.news.model.User;
import ups.glre.news.service.NewsService;


@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@Local
public class NewsServiceImpl implements NewsService {

	@PersistenceContext(unitName = "PersistenceManager")
	protected EntityManager entityManager;

	@EJB
	private NewsDAO newsDAO;
	
	@EJB
	private CategoryDAO categoryDAO;
	
	@EJB
	private UserDAO userDAO;
	
	@EJB
	private NewsJmsService newsJmsService;

	public NewsServiceImpl() {
		
		/*
		 * categoryDAO.createObject(new Category("topic1"));
		 * categoryDAO.createObject(new Category("topic2"));
		 * categoryDAO.createObject(new Category("topic3"));
		 * categoryDAO.createObject(new Category("topic4"));
		 * categoryDAO.createObject(new Category("topic5"));
		 */
		
	}

	@Override
	public News createNews(int userId, int categoryId, String title,
			String content) {
		User user = userDAO.findObjectById(userId);
		Category category = categoryDAO.findObjectById(categoryId);

		News news = new News(user, category, title, content);
		newsDAO.createObject(news);
		
		newsJmsService.sendMessage(category.getCategoryName(), title, user.getLogin(), content, new Date().toString());
		return news;
	}

	@Override
	public News createNews(String login, String categoryName, String title,
			String content) throws Exception {
		User user = userDAO.findUserByLogin(login);
		if (user == null){
			throw new Exception("User with login, " + login + "is not found !");
		}
		
		Category category = categoryDAO.findCategoryByName(categoryName);
		if (category == null){
			throw new Exception("Category, " + categoryName + "is not found !");
		}
		
		News news = new News(user, category, title, content);
		newsDAO.createObject(news);
		newsJmsService.sendMessage(category.getCategoryName(), title, user.getLogin(), content, new Date().toString());
		return news;
	}
	@Override
	public void deleteNews(int newsId) {
		News news = newsDAO.findObjectById(newsId);
		newsDAO.deleteObject(news);
	}

	@Override
	public void deleteNews(News news) {
		newsDAO.deleteObject(news);
	}

	@Override
	public News UpdateNews(int newsId, int categoryId, String title,
			String content) {
		News news = newsDAO.findObjectById(newsId);
		Category category = categoryDAO.findObjectById(categoryId);

		news.setCategory(category);
		news.setContent(content);
		news.setTitle(title);

		newsDAO.updateObject(news);

		return news;
	}

	@Override
	public List<News> getAllNews() {
		List<News> newsList = newsDAO.findAllObject();
		return newsList;
	}

	@Override
	public List<News> getNewsByDate(GregorianCalendar date) {

		return null;
	}

	@Override
	public List<News> getNewsByCategory(int categoryId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<News> getNewsByAuthor(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public News getGetNews(int newsId) {
		News news = newsDAO.findObjectById(newsId);
		return news;
	}

}
